﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using WebExam.Core.Security;

namespace WebExam.Core.Common
{
    public static class Utilities
    {
        private static string _relativeWebRoot;
        private static string _staticResourcePath;

        public static string StaticResourcePath
        {
            get
            {
                if (_staticResourcePath == null)
                {
                    _staticResourcePath = ConfigurationManager.AppSettings["WebExam.StaticResourcePath"];
                }

                return _staticResourcePath;
            }
        }

        public static string RelativeWebRoot
        {
            get
            {
                if (_relativeWebRoot == null)
                {
                    _relativeWebRoot = VirtualPathUtility.ToAbsolute(ConfigurationManager.AppSettings["WebExam.VirtualPath"]);
                }

                return _relativeWebRoot;
            }
        }

        public static string DatabaseConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["WebExam.DBConnection"].ConnectionString;
            }
        }

        public static PasswordFormat PasswordFormat
        {
            get
            {
                String formatString = ConfigurationManager.AppSettings["WebExam.PasswordFormat"];
                PasswordFormat format = PasswordFormat.Clear;

                switch (formatString)
                {
                    case "Encrypted":
                        format = PasswordFormat.Encrypted;
                        break;
                    case "Hashed":
                        format = PasswordFormat.Hashed;
                        break;
                    default:
                        break;
                }

                return format;
            }
        }

        public static decimal ToMoney(this decimal price)
        {
            return Decimal.Round(price, 2);
        }

        //截取字符串
        public static string SubStr(this string str, int len)
        {
            if (str.Length <= len) return str;
            return str.Substring(0, len - 1);
        }

        //截取字符串
        public static string SubStr(this string str, int len, string mark)
        {
            if (str.Length <= len)
                return str;
            return str.Substring(0, len - 1) + mark;
        }

        public static string Escape(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;
            var sb = new StringBuilder();
            var ba = Encoding.Unicode.GetBytes(s);
            for (var i = 0; i < ba.Length; i += 2)
            {
                if (ba[i + 1] == 0)
                {
                    //数字,大小写字母,以及"+-*/._"不变
                    if (
                        (ba[i] >= 48 && ba[i] <= 57)
                        || (ba[i] >= 64 && ba[i] <= 90)
                        || (ba[i] >= 97 && ba[i] <= 122)
                        || (ba[i] == 42 || ba[i] == 43 || ba[i] == 45 || ba[i] == 46 || ba[i] == 47 || ba[i] == 95)
                        )//保持不变
                    {
                        sb.Append(Encoding.Unicode.GetString(ba, i, 2));
                    }
                    else//%xx形式
                    {
                        sb.Append("%");
                        sb.Append(ba[i].ToString("X2"));
                    }
                }
                else
                {
                    sb.Append("%u");
                    sb.Append(ba[i + 1].ToString("X2"));
                    sb.Append(ba[i].ToString("X2"));
                }
            }
            return sb.ToString();
        }
    }
}